(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.piUpload.bankVerifyController', bankVerifyController);
	bankVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'piUploadService'];
	function bankVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, piUploadService) {
		var ctrl = this, param = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piLoad = {};
		
		ctrl.init = function() {
		
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.formConfigs.pi.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.gridOptions.multiSelect = true;
			ctrl.gridOptions.multiPageSelect = true;
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.searchCriteria.piloadId = param.dataKey;
			authService.bankVerify(param).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.piLoad = response.data.model;
					ctrl.piLoad.taskId = response.data.task.taskId;
					if(ctrl.piLoad.approveCount > 1){
						ctrl.gridOptions.multiSelect = false;
						ctrl.gridOptions.multiPageSelect = false;
						ctrl.gridOptions.enableRowSelection = false;
					}
					ctrl.search();
				}
			});
		};
		
		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			if(!ctrl.searchCriteria.sortBy){
				ctrl.searchCriteria.sortBy = "PI_ID";
				ctrl.searchCriteria.sortDirection = "desc";
			}
			if(ctrl.piLoad.loadStatus){
				ctrl.searchCriteria.piStatus = ctrl.piLoad.loadStatus;
			}else{
				ctrl.searchCriteria.piStatus = "L";
			}
			
			authService.getPiList(ctrl.searchCriteria).then(
				function(response) {
					angular.merge(ctrl.searchCriteria, response.data);
					gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
				},angular.noop);
		};
		
		ctrl.bankVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.piLoad.comments = comments;
						authService.bankVerifyReject(ctrl.piLoad).then(rejectSuccessful, failed);
					}
				}
			});
		};

		ctrl.bankVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.piLoad.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.bankVerifyApprove(ctrl.piLoad).then(successful, failed);
					}
				}
			});
		};

		function successful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}]
			});
		}
		
		
		function failed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}]
			});
		}

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}
		
		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.piLoad.piList = selectedRows;
		}
		
		ctrl.getAttachmentList = function() {
			piUploadService.getAttachmentList(ctrl.piLoad.currentDataType, ctrl.piLoad.piloadId, false);
		};
		
	}
})(angular);